package com.tenton.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Date: 2021/3/29 16:38
 * @Author: Tenton
 * @Description: 时间包
 */
public class DateUtils {
    /**
     * 设置开始时间
     * @param date
     * @return
     */
    public static Date setStartDate(Date date){
        if (null == date) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        if (!(StringUtils.isEmpty(format))) {
            String dateStr = format + " 00:00:00";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                return simpleDateFormat.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    /**
     * 设置结束时间
     * @param date
     * @return
     */
    public static Date setEndDate(Date date){
        if (null == date) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        if (!(StringUtils.isEmpty(format))) {
            String dateStr = format + " 23:59:59";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                return simpleDateFormat.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    /**
     * 获取格式为yyyy-MM-dd的时间
     * @param date
     * @return
     */
    public static String  getYmdDate(Date date){
        if (null == date) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 获取前一天时间
     * @param nowDate
     * @return
     */
    public static Date getYesterday(Date nowDate){
        if (nowDate == null){
            return null;
        }
        //前一天时间戳
        long time = nowDate.getTime() - (24 * 60 * 60 * 1000);
        //时间戳转换为时间
        Date d = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(sdf.format(d));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
